package gestion.camping.vue;

import gestion.camping.controleur.ReservationController;
import gestion.camping.dto.Reservation;
import gestion.camping.dto.StatutReservation;
import gestion.camping.utils.DateFormatUtils;
import gestion.camping.utils.MessagesUtil;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class ViewReservationListe extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1923064145354238975L;
	
	private TableReservation table;

	private List<Reservation> listReservation;
	private ReservationController controller;
	private JComboBox comboBoxStatut;
	private JButton btnRechercher;
	private JButton btnValider;
	private JButton btnAjouter;
	private JLabel lblStatut;
	private List<StatutReservation> listStatut;
	private JLabel lblId;
	private JTextField textFieldId;
	private JLabel lblDateDebut;
	private JTextField textFieldDateDebut;
	private JLabel lblDateFin;
	private JTextField textFieldDateFin;

	/**
	 * Create the panel.
	 */
	public ViewReservationListe(ReservationController controller) {
	
		this.controller = controller;
		
		this.listReservation = controller.getListReservation();
		
		initCompoment();
		
		setListStatut(controller.getListStatutReservation());
	}
	
	public void initCompoment(){
	
		setBounds(0, 25, 1150, 700);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.15, 0.2, 1.0, 0.0, 1.0, 0.0, 1.0, 0.2, 0.15, 0.15};
		gridBagLayout.rowWeights = new double[]{0.1, 0.0, 0.0, 0.0, 0.0, 0.5, 0.1};
		setLayout(gridBagLayout);
		
		lblId = new JLabel(MessagesUtil.formatMessage("reservation.liste.critere.numero"));
		GridBagConstraints gbc_lblId = new GridBagConstraints();
		gbc_lblId.anchor = GridBagConstraints.EAST;
		gbc_lblId.insets = new Insets(0, 0, 5, 5);
		gbc_lblId.gridx = 1;
		gbc_lblId.gridy = 2;
		add(lblId, gbc_lblId);
		
		textFieldId = new JTextField();
		GridBagConstraints gbc_textFieldId = new GridBagConstraints();
		gbc_textFieldId.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldId.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldId.gridx = 2;
		gbc_textFieldId.gridy = 2;
		add(textFieldId, gbc_textFieldId);
		textFieldId.setColumns(10);
		
		lblDateDebut = new JLabel(MessagesUtil.formatMessage("reservation.liste.critere.dateDebut"));
		GridBagConstraints gbc_lblDateDebut = new GridBagConstraints();
		gbc_lblDateDebut.anchor = GridBagConstraints.EAST;
		gbc_lblDateDebut.insets = new Insets(0, 0, 5, 5);
		gbc_lblDateDebut.gridx = 3;
		gbc_lblDateDebut.gridy = 2;
		add(lblDateDebut, gbc_lblDateDebut);
		
		textFieldDateDebut = new JTextField();
		GridBagConstraints gbc_textFieldDateDebut = new GridBagConstraints();
		gbc_textFieldDateDebut.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldDateDebut.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldDateDebut.gridx = 4;
		gbc_textFieldDateDebut.gridy = 2;
		add(textFieldDateDebut, gbc_textFieldDateDebut);
		textFieldDateDebut.setColumns(10);
		
		lblDateFin = new JLabel(MessagesUtil.formatMessage("reservation.liste.critere.dateFin"));
		GridBagConstraints gbc_lblDateFin = new GridBagConstraints();
		gbc_lblDateFin.anchor = GridBagConstraints.EAST;
		gbc_lblDateFin.insets = new Insets(0, 0, 5, 5);
		gbc_lblDateFin.gridx = 5;
		gbc_lblDateFin.gridy = 2;
		add(lblDateFin, gbc_lblDateFin);
		
		textFieldDateFin = new JTextField();
		GridBagConstraints gbc_textFieldDateFin = new GridBagConstraints();
		gbc_textFieldDateFin.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldDateFin.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldDateFin.gridx = 6;
		gbc_textFieldDateFin.gridy = 2;
		add(textFieldDateFin, gbc_textFieldDateFin);
		textFieldDateFin.setColumns(10);
		
		lblStatut = new JLabel(MessagesUtil.formatMessage("reservation.liste.critere.statutReservation"));
		GridBagConstraints gbc_lblSaison = new GridBagConstraints();
		gbc_lblSaison.anchor = GridBagConstraints.EAST;
		gbc_lblSaison.insets = new Insets(0, 0, 5, 5);
		gbc_lblSaison.gridx = 7;
		gbc_lblSaison.gridy = 2;
		add(lblStatut, gbc_lblSaison);
		
		comboBoxStatut = new JComboBox();
		GridBagConstraints gbc_comboBoxSaison = new GridBagConstraints();
		gbc_comboBoxSaison.insets = new Insets(0, 0, 5, 5);
		gbc_comboBoxSaison.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBoxSaison.gridx = 8;
		gbc_comboBoxSaison.gridy = 2;
		add(comboBoxStatut, gbc_comboBoxSaison);
		
		btnRechercher = new JButton(MessagesUtil.formatMessage("reservation.liste.btn.rechercher"));
		btnRechercher.addActionListener(this);
		GridBagConstraints gbc_btnRechercher = new GridBagConstraints();
		gbc_btnRechercher.anchor = GridBagConstraints.EAST;
		gbc_btnRechercher.insets = new Insets(0, 0, 5, 5);
		gbc_btnRechercher.gridx = 8;
		gbc_btnRechercher.gridy = 4;
		add(btnRechercher, gbc_btnRechercher);
				
		table = new TableReservation();
		
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.gridwidth = 8;
		gbc_table.insets = new Insets(0, 0, 5, 5);
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 1;
		gbc_table.gridy = 5;
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, gbc_table);
		
		btnAjouter = new JButton(MessagesUtil.formatMessage("reservation.ajout.title"));
		btnAjouter.addActionListener(this);
		GridBagConstraints gbc_btnAjouter = new GridBagConstraints();
		gbc_btnAjouter.anchor = GridBagConstraints.EAST;
		gbc_btnAjouter.insets = new Insets(0, 0, 0, 5);
		gbc_btnAjouter.gridx = 7;
		gbc_btnAjouter.gridy = 6;
		add(btnAjouter, gbc_btnAjouter);
		
		btnValider = new JButton(MessagesUtil.formatMessage("reservation.liste.btn.modifier"));
		btnValider.addActionListener(this);
		GridBagConstraints gbc_btnValider = new GridBagConstraints();
		gbc_btnValider.anchor = GridBagConstraints.WEST;
		gbc_btnValider.insets = new Insets(0, 0, 0, 5);
		gbc_btnValider.gridx = 8;
		gbc_btnValider.gridy = 6;
		add(btnValider, gbc_btnValider);
		
	}

	public List<Reservation> getListReservation() {
		return listReservation;
	}

	public void setListReservation(List<Reservation> listReservation) {
		this.listReservation = listReservation;
		
		update();
	}
	
	private void update(){
		
		//Mise à jour de la liste déroulante
	    //Supprime tout les anciens éléments
		comboBoxStatut.removeAllItems();
		
		//On ajoute le choix vide
		comboBoxStatut.addItem(null);
		
		// On insère ensuite les nouvelles valeurs
		for(StatutReservation statut : listStatut){
			comboBoxStatut.addItem(statut.getLibelle());
		}
		
		//On pr�selectionne l'�l�ment
		int i;
		if(controller.getStatutReservation() != null){
			for(i=0; i < listStatut.size() && listStatut.get(i).getId() != controller.getStatutReservation().getId(); i++);
			if(i < listStatut.size()){
				comboBoxStatut.setSelectedIndex(i+1);
			}
		}
		
		
		int indexElementSelected = table.getSelectedRow();
		
		table.setModel(new TableReservationModel(listReservation, table.getColumnNames()));
		table.setShowGrid(true);
		table.setVisible(true);
		
		this.repaint();
		
		if(indexElementSelected >= 0){
			table.setRowSelectionInterval(indexElementSelected, indexElementSelected);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnRechercher){
			StatutReservation statut = comboBoxStatut.getSelectedItem() == null ? null : listStatut.get(comboBoxStatut.getSelectedIndex()-1);
			Integer id = textFieldId.getText()== null || textFieldId.getText().equals("") ? null : Integer.valueOf(textFieldId.getText());
			this.controller.rechercher(statut, DateFormatUtils.parseDate(textFieldDateDebut.getText()), 
										DateFormatUtils.parseDate(textFieldDateFin.getText()), id);
		}else if(e.getSource() == btnValider){
			Reservation selectedPersonne = listReservation.get(this.table.getSelectedRow());			
			
			this.controller.modifier(selectedPersonne);
		}else if(e.getSource() == btnAjouter){
			this.controller.ajouter();
		}
		
	}
	
	public Reservation getSelectedPersonne(){	
		return table.getSelectedRow() < 0 ? null : listReservation.get(table.getSelectedRow());
	}

	public void setListStatut(List<StatutReservation> listStatut) {
		this.listStatut = listStatut;
		
		update();
	}

	public List<StatutReservation> getListStatut() {
		return listStatut;
	}

}
